# coding=utf-8
"""
Author: Mr.夏末
@Function: 文件上传与下载
"""
import requests

url = 'https://graph.baidu.com/upload'
files1 = {'image': ('nn.jpg', open('../lib/cases/电商项目用例.xlsx', 'rb'))}  # 直接通过open函数打开文件并将文件对象存在字典中
r = requests.post(url, files=files1)
print(r.text)

files2 = {'image': open('../lib/mm.jpg', 'rb')}  # 直接通过open函数打开文件并将文件对象存在字典中
r = requests.post(url, files=files2)
print(r.text)

print("**********************多文件上传1********************************")

url = 'http://httpbin.org/post'
# 打开上传文件并且加入文件相关参数
files = {
    "field1": ("filename1", open('../lib/cases/电商项目用例.xlsx', 'rb')),
    "field3": ('report.xls', open('../lib/cases/电商项目用例.xlsx', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})
}
# 其他参数可以同时使用data=或者json=补充传递
data = None
json = None
r = requests.post(url, data=data, json=json, files=files)
print(r.text)

print("**********************多文件上传2********************************")

url = 'http://httpbin.org/post'
files = [
    ("field1", ("filename1", open('../lib/cases/电商项目用例.xlsx', 'rb'))),
    ("field3", ('哼哼哈嘿.xls', open('../lib/cases/电商项目用例.xlsx', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'}))
]
r = requests.post(url, data=data, json=json, files=files)
print(r.text)
